<?php
/**
 * 用车单控制器
 * 
 * @category QuickBug
 * @copyright http://www.vquickbug.com
 */
class OrderCarController extends BaseController
{
	/**
	 * 模型
	 *
	 * @var unknown_type
	 */
	private $model;
	
	/**
	 * 自动运行
	 */
	public function init(){
		parent::init();
		$this->model = new Model_Order_Car();
	}

	/**
	 * 首页
	 */
	public function listAction(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$date1 = (isset($_POST['date1']) && !empty($_POST['date1'])) ? $_POST['date1'] : '';
		$date2 = (isset($_POST['date2']) && !empty($_POST['date1'])) ? $_POST['date2'] : '';
		$num = (isset($_POST['num']) && !empty($_POST['num'])) ? mysql_real_escape_string($_POST['num']) : '';
		$offset = ($page-1)*$rows;
		
		if($num){
			$num_arr = explode('TRA', $num, 2);
			$id = intval($num_arr[1]);
		}
		else{
			$id=0;
		}
		
		$param = array(
			'id'=>$id,
			'uid'=>0,
			'date1'=>$date1,
			'date2'=>$date2,
			'offset'=>$offset,
			'rows'=>$rows,
		);
		$result = $this->model->listOrderCar($param);
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function comboboxAction(){
		$result = array();
		$result = $this->model->comboboxUser();
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function addAction(){
		$param = array(
			'uid'=>intval($this->request->getPost('uid')),
			'uname'=>$this->model->getUname(intval($this->request->getPost('uid'))),
			'driver'=>$this->request->getPost('driver'),
			'car_id'=>$this->request->getPost('car_id'),
			'destination'=>$this->request->getPost('destination'),
			'amount_estimated'=>$this->request->getPost('amount_estimated'),
			'amount_real'=>$this->request->getPost('amount_real'),
			'description'=>$this->request->getPost('description'),
			'colleague'=>$this->request->getPost('colleague'),
			'comment'=>$this->request->getPost('comment'),
			'date'=>date("Y-m-d"),
			'time'=>date("H:i:s"),
		);
		
		$result = $this->model->addOrderCar($param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'添加申请失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function updateAction(){
		$id = intval($this->request->getGet('id'))?intval($this->request->getGet('id')):0;
		$param = array(
			'uid'=>intval($this->request->getPost('uid')),
			'uname'=>$this->model->getUname(intval($this->request->getPost('uid'))),
			'driver'=>$this->request->getPost('driver'),
			'car_id'=>$this->request->getPost('car_id'),
			'destination'=>$this->request->getPost('destination'),
			'amount_estimated'=>$this->request->getPost('amount_estimated'),
			'amount_real'=>$this->request->getPost('amount_real'),
			'description'=>$this->request->getPost('description'),
			'colleague'=>$this->request->getPost('colleague'),
			'comment'=>$this->request->getPost('comment'),
		);

		$result = $this->model->updateOrderCar($id,$param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'编辑申请单失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function delAction(){
		$id = intval($this->request->getPost('id'))?intval($this->request->getPost('id')):0;
		$result = $this->model->delOrderCar($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'取消申请单失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function checkoutAction(){
		$id = intval($this->request->getGet('id'))?intval($this->request->getGet('id')):0;
		$param = array(
			'amount_real'=>$this->request->getPost('amount_real'),
			'is_paid'=>$this->request->getPost('is_paid')?1:0,
		);
		if($param['is_paid']){
			$param['paid_at']=time();
			$param['paid_by']=$this->userid;
		}

		$result = $this->model->updateOrderCar($id,$param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'结算处理失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function sumAction(){
		$date1 = (isset($_POST['date1'])&&!empty($_POST['date1'])) ? $_POST['date1'] : date('Y-m').'-01';
		$date2 = (isset($_POST['date2'])&&!empty($_POST['date1'])) ? $_POST['date2'] : date('Y-m-d');
		$result = array();
		$result["total"] = 1;
		$result["rows"] = $this->model->sumOrderCar($date1,$date2);
		$result["footer"] = $this->model->totalOrderCar($date1,$date2);
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
}